package com.yongdin.minimalism;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.google.gson.Gson;
import com.tamic.novate.Novate;
import com.tamic.novate.Throwable;
import com.tamic.novate.callback.RxStringCallback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by aruba on 2017/11/6.
 * 天气
 */

public class WeatherActivity extends AppCompatActivity {
    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    private WeatherCallBack weatherCallBack;
    private final String change = "转";
    private final String tempflag = "℃";
    private final String to = "-";
    private Map parameters = new HashMap();
    //声明定位回调监听器
    public AMapLocationListener mLocationListener = new AMapLocationListener() {


        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation != null && aMapLocation.getErrorCode() == 0) {
                parameters.put("city", aMapLocation.getCity());

                novate.rxGet("v3/weather/weatherInfo", parameters, new RxStringCallback() {
                    @Override
                    public void onNext(Object tag, String response) {
                        WeatherInfo weatherInfo = new Gson().fromJson(response, WeatherInfo.class);
                        if (weatherCallBack != null && weatherInfo != null) {
                            if (weatherInfo.forecasts == null || weatherInfo.forecasts.size() == 0) {
                                return;
                            }
                            List<Cast> casts = weatherInfo.forecasts.get(0).casts;
                            if (casts.size() > 0) {
                                Cast cast = casts.get(0);
                                String weather;
//                                if (cast.dayweather.equals(cast.nightweather)) {
//                                    weather = cast.dayweather;
//                                } else {
//                                    weather = cast.dayweather + change + cast.nightweather;
//                                }
                                weatherCallBack.callBack(weatherInfo.forecasts.get(0).city, cast);
                            }
                        }
                    }

                    @Override
                    public void onError(Object tag, Throwable e) {

                    }

                    @Override
                    public void onCancel(Object tag, Throwable e) {

                    }
                });
            }
        }
    };
    private AMapLocationClientOption locationOption = null;

    private Novate novate;
    private String baseUrl = "http://restapi.amap.com/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        parameters.put("key", "2ea7b083de238fa7e7899d8c89d0b681");
        parameters.put("extensions", "all");
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        locationOption = getDefaultOption();
        //设置定位参数
        mLocationClient.setLocationOption(locationOption);
        novate = new Novate.Builder(this)
                .baseUrl(baseUrl)
                .build();
    }

    public void setWeatherCallBack(WeatherCallBack weatherCallBack) {
        this.weatherCallBack = weatherCallBack;
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 启动定位
        mLocationClient.startLocation();
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 停止定位
        mLocationClient.stopLocation();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLocationClient.onDestroy();
    }

    /**
     * 默认的定位参数
     */
    private AMapLocationClientOption getDefaultOption() {
        AMapLocationClientOption mOption = new AMapLocationClientOption();
        mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选，设置定位模式，可选的模式有高精度、仅设备、仅网络。默认为高精度模式
        mOption.setGpsFirst(false);//可选，设置是否gps优先，只在高精度模式下有效。默认关闭
        mOption.setHttpTimeOut(30000);//可选，设置网络请求超时时间。默认为30秒。在仅设备模式下无效
        mOption.setInterval(3 * 60 * 1000);//可选，设置定位间隔。默认为2秒
        mOption.setNeedAddress(true);//可选，设置是否返回逆地理地址信息。默认是true
        mOption.setOnceLocation(false);//可选，设置是否单次定位。默认是false
        mOption.setOnceLocationLatest(false);//可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
        AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选， 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
        mOption.setSensorEnable(false);//可选，设置是否使用传感器。默认是false
        mOption.setWifiScan(true); //可选，设置是否开启wifi扫描。默认为true，如果设置为false会同时停止主动刷新，停止以后完全依赖于系统刷新，定位位置可能存在误差
        mOption.setLocationCacheEnable(true); //可选，设置是否使用缓存定位，默认为true
        return mOption;
    }

    class WeatherInfo {
        public String status;
        public String count;
        public String info;
        public String infocode;
        public List<Forecast> forecasts;
    }

    class Forecast {
        public String province;
        public String city;
        public String reporttime;
        public String adcode;
        public List<Cast> casts;

    }

    class Cast {
        public String date;
        public String dayweather;
        public String nightweather;
        public String daytemp;
        public String nighttemp;
    }

    public interface WeatherCallBack {
        void callBack(String city, Cast cast);
    }

}
